// pages/seal/sealorder.js
Page({
  mixins: [require('../mixin/themeChanged')],

  /**
   * 页面的初始数据
   */
  data: {
    type:["合同","文件"],
    typeindex:"",
    project:["请选择"],
    projectindex:"",
    name:"",
    aname:"",
    bname:"",
    filename:"",
    filenum:"",
    dfnum:"",
    cwnum:"",
    cdnum:"",
    num:"",
    content:[],
    list: [
      {
          id: '1',
          name: '查看已申请信息',
          pages: [{sn:"测试会议",url:"../thing/thingorder"}]
      },
     ],

    
  },


  bindTypeChange: function (e) {
    this.setData({
      typeindex: e.detail.value
    })
  },


  bindProjectChange: function (e) {
    this.setData({
      projectindex: e.detail.value
    })
  },

  getname: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  getaname: function (e) {
    this.setData({
      aname: e.detail.value
    })
  },

  getbname: function (e) {
    this.setData({
      bname: e.detail.value
    })
  },

  getfilename: function (e) {
    this.setData({
      filename: e.detail.value
    })
  },

  getfilenum: function (e) {
    this.setData({
      filenum: e.detail.value
    })
  },

  getdfnum: function (e) {
    this.setData({
      dfnum: e.detail.value
    })
  },

  getcwnum: function (e) {
    this.setData({
      cwnum: e.detail.value
    })
  },

  getcdnum: function (e) {
    this.setData({
      cdnum: e.detail.value
    })
  },

  getnum: function (e) {
    this.setData({
      num: e.detail.value
    })
  },

  addHtOrder:function(e){
    var that =this
    var content=[]
    var type=this.data.type[this.data.typeindex]
    var name=this.data.name
    var aname=this.data.aname
    var bname=this.data.bname
    var filename=this.data.filename
    var filenum=Number(this.data.filenum)
    var dfnum=Number(this.data.dfnum)
    var cwnum=Number(this.data.cwnum)
    var cdnum=Number(this.data.cdnum)
    var num = dfnum+cwnum+cdnum
    var project=this.data.project[this.data.projectindex]
    content=this.data.content
    

    content.push({type:type,name:name,aname:aname,bname:bname,filename:filename,filenum:filenum,dfnum:dfnum,cwnum:cwnum,cdnum:cwnum,num:num,project:project})
 
    this.setData({
      content:content
    })

  },


  addFileOrder:function(e){
    var that =this
    var content=[]
    var type=this.data.type[this.data.typeindex]
    var name=this.data.name
    var num = Number(this.data.num)
    content=this.data.content
    

    content.push({type:type,name:name,num:num})

    this.setData({
      content:content
    })

  },

  deleteSealOrder:function(e){
    var content=[]
    content=this.data.content
    content.splice(e.detail.value,1)
    this.setData({
      content:content
    })
  },

  postSealOrder:function(e){ 
    var that=this
    var url = wx.getStorageSync("config") 
    var content=JSON.stringify(this.data.content)

    wx.requestSubscribeMessage({
      tmplIds: ['MwWGcwZYx2EwvFJTx4_D7hVTB6X32lwsQXApFZGmlUY'],
      success (res) {wx.showModal({
        title: '提交确认',
        content: '请核对申请用印内容是否正确',
        success: function(res) {
    
         if (res.confirm) {   
           
        
          wx.request({
            url: url.service.homeUrl+'/seal/sealorder',
            data: { 
              content:content,
              uid:wx.getStorageSync('uid'),      
                  },
  
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {
             
              wx.navigateTo({url: '../seal/sealorder',})
              
              wx.showToast({
                title: res.data.msg,
                icon:"none"
              }) 
            },
            fail: function (res) {
              wx.showToast({
                title: res.data.msg,
              }) 
             },
            complete: function (res) { },
          })
         
         }
        }
       }) }
    })
 

    
  },
  
  kindToggle: function (e) {
    var id = e.currentTarget.id, list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
        if (list[i].id == id) {
            list[i].open = !list[i].open
        } else {
            list[i].open = false
        }
    }
    this.setData({
        list: list
    });
},



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    var url = wx.getStorageSync("config") 

    wx.request({
      url: url.service.homeUrl+'/seal/getproject',
      data: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {

        for(var i=0;i<res.data.length;i++)
        { var newarray = [res.data[i].project,];        
         that.data.project = that.data.project.concat(newarray);
         that.setData({
          project: that.data.project
        });  
      }},
      fail: function (res) {console.log(res) },
      complete: function (res) { console.log(res) },
    })


    var thing='list[0].pages'
    wx.request({     
      url: url.service.homeUrl+'/seal/usergetsealorderlist',
      data: { 
              uid:wx.getStorageSync('uid')
            },

      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        
        this.setData({[thing]: res.data})
        console.log(that.data.list)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },






 


  changeTheme: function () {
    console.log(this.data);
    getApp().themeChanged(this.data.theme === 'light' ? 'dark' : 'light');
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})